package org.lanqiao.netctoss.controller.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.netctoss.entity.Admin;
import org.lanqiao.netctoss.entity.Permiss;
import org.lanqiao.netctoss.service.impl.AdminServiceImpl;
import org.lanqiao.netctoss.service.impl.AdperServiceImpl;

/**
 * Servlet implementation class UpdateAdminServlet
 */
public class UpdateAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateAdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		//response.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("GBK");
		int adm_no = Integer.parseInt(request.getParameter("adm_no"));
		String name=request.getParameter("name");
		String tel=request.getParameter("tel");
		String email=request.getParameter("email");
		String[] permiss = request.getParameterValues("permiss");
		List<Permiss> lp = new ArrayList<Permiss>();
		for(String per_no : permiss){
			Permiss permis = new Permiss();
			permis.setPer_no((Integer.parseInt(per_no)));
			lp.add(permis);
		}
		/*boolean b = adminBuss.updAdmin(new Admin(name,adm_no,tel,email));
		boolean d = adperBuss.deleteAdperByAno(new Admin(adm_no));
		adminBuss.insertAdper(new Admin(adm_no,lp));*/
		 int a =  new AdminServiceImpl().updateAdmin(new Admin(name,adm_no,tel,email));
		 boolean b = new AdminServiceImpl().deleteAdperByAno(new Admin(adm_no));
		 int c =new AdminServiceImpl().insertAdper(new Admin(adm_no,lp));
		 if(a>0&&c>0&&b){
			PrintWriter out = response.getWriter();
			out.print("<script>alert('修改成功!');window.location.href='../admin/AdminListServlet'</script>");
		 }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
